#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
import re
import pepe.p_utils.oracle_dmp as odmp

def oracle_log_export_done_in(f):
    """Extract encoding used when exporting Oracle table from Oracle log file

    Example: 'Export done in UTF8 character set and UTF8 NCHAR character set'
    
    """
    fh = open(f, 'rb')
    lines = fh.readlines()
    fh.close()
    
    for line in lines:
        if re.sub('^Export done in ', '', line[:15]) == '':
            line = re.sub('\x0D', '', line)
            line = re.sub('\x0A', '', line)
            return line
    return ''


def rows_in_dmp(f):
    """Returns number of rows from Oracle dmp files

    Works for Oracle dmp version 10+ 
    METRICET string not present in Oracle v9-

    """
    return odmp.srchMetricet(f)

    if 0: # procedure test - ok
        print rows_in_dmp('R01_BSIS_RU40_2010.dmp')
        print rows_in_dmp('R01_BSIS_RU40_2010.dmp')[9:]
        '''
        METRICET 28392
        28392
        '''


def examine_oracle_log(f):
    """Check files with .log extension (Oracle log file) 
    and try to extract information how many rows were exported.

    Example of log file:

        Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
        With the Partitioning, OLAP, Data Mining and Real Application Testing options
        Export done in UTF8 character set and UTF8 NCHAR character set
        
        About to export specified tables via Conventional Path ...
        Current user changed to SAPSR3
        . . exporting table                           BSAD          0 rows exported
        Export terminated successfully without warnings.

    """
    r = {'ora_log_success': '', 'rows_exported': ''}
    if os.path.exists(f) and os.path.splitext(f)[1].lower in ('.log'):
        data = open(f, 'r').read()

        if re.search('Export terminated successfully without warnings', data):
            r['ora_log_success'] = 's'
        elif re.search('successfully completed at \d{2}\:\d{2}\:\d{2}\x0A', data):
            r['ora_log_success'] = 's'
        elif re.search('Export terminated successfully with warnings', data):
            r['ora_log_success'] = 'sw'
        elif re.search('Export terminated unsuccessfully', data):
            r['ora_log_success'] = 'u'
            
        s3 = re.search("(EXP.\d{5}.{1,})\x0A", data)
        if s3: 
            r['rows_exported'] = str(s3.group(1))

        s2 = re.search("(.{11}ORACLE error.{1,})\x0A", data)
        if s2: 
            r['rows_exported'] = str(s2.group(1))

        s1 = re.search("(\d{1,}) rows exported", data)
        if s1: 
            r['rows_exported'] = str(s1.group(1))

    return r
        
    if 0: # testing procedure - ok
        # checking nonexistent file
        print examine_oracle_log(r'A:\SAP LH1\2010-10-11\original\Cherly_RU40_LH1\LH1_BSIS_RU40_2010.log_')
        # checking existent and OK log file
        print examine_oracle_log(r'A:\SAP LH1\2010-10-11\original\Cherly_RU40_LH1\LH1_BSIS_RU40_2010.log')

